<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yxList.length === 1" allowClear v-model="queryParam.ssyxm" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="专业">
              <a-select allowClear v-model="queryParam.sszym" showSearch :filterOption="filterOption">
                <a-select-option v-for="(i,index) in selectMenu.courseList" :key="index" :value="i.zyh">{{i.zyh}}-{{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select v-model="queryParam.pyccm" allowClear>
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="年级"
              >
                <a-select v-model="queryParam.sznj" allowClear>
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="学年"
              >
                <a-select v-model="queryParam.xn" allowClear>
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                  label="学制"
              >
                <a-select v-model="queryParam.xz" allowClear>
                  <a-select-option v-for="i in TYPE0301" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="12">
              <a-form-item
                label="考试学期"
              >
                <a-select v-model="queryParam.ksxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :md="6" :sm="12">
              <a-form-item
                label="考试性质"
              >
                <a-select v-model="queryParam.ksxz">
                  <a-select-option v-for="i in TYPE0358" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <!-- <a-col :md="6" :sm="12">
              <a-form-item
                label="考试类别"
              >
                <a-select v-model="queryParam.kslb">
                  <a-select-option v-for="i in TYPE0109" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="6" :sm="12">
              <a-form-item
                label="学号"
              >
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                label="姓名"
              >
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学籍状态">
                <a-select allowClear v-model="queryParam.xjztm"  placeholder="请选择">
                  <a-select-option v-for="i in TYPE0073" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="排名">
                <a-select allowClear v-model="queryParam.sortField" default-value="pjjd" placeholder="请选择">
                  <a-select-option value="pjcj">平均分排名</a-select-option>
                  <a-select-option value="pjjd">平均绩点排名</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="exportExcel" style="margin:0 10px">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="action" slot-scope="text, record">
          <a-popconfirm title="确定修改为重修状态吗？" @confirm="changeStatus(record.cjksxsmdId, '04')">
            <a>重修</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a-popconfirm title="确定修改为补考状态吗？" @confirm="changeStatus(record.cjksxsmdId, '06')">
            <a>补考</a>
          </a-popconfirm>
        </template>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '学号',
          dataIndex: 'xsxh'
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '年级',
          dataIndex: 'ksnjmc'
        },
        // {
        //   title: '学年',
        //   dataIndex: 'xn'
        // },
        {
          title: '院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '专业',
          dataIndex: 'sszymc'
        },
        {
          title: '平均绩点',
          dataIndex: 'pjjd'
        },
        {
          title: '平均分',
          dataIndex: 'pjcj'
        },
        // {
        //   title: '操作',
        //   dataIndex: 'id',
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.pjcjList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {
        yxList:[]
      },
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0126: state => state.app['TYPE0126'],
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0072: state => state.app['TYPE0072'],
      TYPE0109: state => state.app['TYPE0109'],
      TYPE0358: state => state.app['TYPE0358'],
      TYPE0073: state => state.app['TYPE0073'],
      TYPE0301: state => state.app['TYPE0301'],
      TYPE0075: state => state.app['TYPE0075']
      
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0109')
    this.$store.dispatch('app/setDictionary', 'TYPE0358')
    this.$store.dispatch('app/setDictionary', 'TYPE0073')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0301')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]

      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        this.$set(this.queryParam, 'sznj', res[1].currentGrade)
        this.$set(this.queryParam, 'sortField', 'pjjd')
        // this.$set(this.queryParam, 'ksxq', res[1].currentSemester)
        this.$set(this.queryParam, 'xn', res[1].schoolYear)
        if(this.selectMenu.yxList && this.selectMenu.yxList.length == 1){
          this.$set(this.queryParam, 'kcyx', this.selectMenu.yxList[0].dwh)
          this.selectMenu.yxList.unshift({
            dwh: '000',
            dwmc: '研究生院',
            id: '874ce5e7424c446c831c45a34a0c30cc'
          })
          this.yxChange(this.queryParam.kcyx)
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.selectMenu, 'kcbm', '')
      this.$set(this.selectMenu, 'courseList', [])
      if (code) {
        this.kcLoading = true
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'courseList', res.data)
        }).finally(() => {
          this.kcLoading = false
        })
      } else {
        this.$set(this.selectMenu, 'courseList', [])
      }
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    changeStatus (id, xkzt) {
      this.$api.cultivate.scoreFailChange({id, xkzt}).then(res => {
        this.$mnessage.success(res.msg)
        this.$refs.table.refresh(false)
      })
    },
    exportExcel () {
      let params = {...this.queryParam}
      delete params.pageNum
      this.$export('/foster/kccj/exportPjcj', params)
    }
  }
}
</script>

<style>

</style>
